﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiniStock.Client.UI.CommandService;
using MiniStock.Client.UI.Models;
using MiniStock.Client.UI.QueryService;

namespace MiniStock.Client.UI.Controllers
{
    public class OrderController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create(Guid? productId, Guid? customerId)
        {
            var model = new CreateOrderModel
            {
                ProductId = productId,
                CustomerId = customerId,
            };
            return View(model);
        }

        public ActionResult Get(Guid id)
        {
            var service = new StockServiceFacadeClient();
            var result = service.GetOrder(id);
            return View(result);
        }

        public ActionResult List()
        {
            var service = new StockServiceFacadeClient();
            var result = service.ListOrders(null);
            return View(result);
        }

        public ActionResult Search(string customerId, string productId)
        {
            var service = new StockServiceFacadeClient();
            var queryToken = new QueryToken()
            {
                Query = new List<FilterElement>(),
                OrderBy = "Name",
                PageSize = 100,
                PageIndex = 0,
            };

            if (!String.IsNullOrEmpty(customerId))
                queryToken.Query.Add(
                    new FilterElement { FieldName = "CustomerId", Operator = QueryOperator.Equals, FieldValue = String.Format("'{0}'", customerId) });
            if (!String.IsNullOrEmpty(productId))
                queryToken.Query.Add(
                    new FilterElement { FieldName = "OrderItem_ProductId", Operator = QueryOperator.Equals, FieldValue = String.Format("'{0}'", productId) });

            var result = service.ListOrders(queryToken);
            return Json(result.Select(i =>
                                          new
                                          {
                                              value = i.Id,
                                              label = String.Format("{0} {1}", i.Customer.Name, i.OrderDeadline),
                                          }).ToList(), JsonRequestBehavior.AllowGet);
        }
        
        [HttpPost]
        public Guid Create(CreateOrderModel model)
        {
            var service = new ServiceFacadeClient();
            var result = service.SubmitEvent(new CreateOrderEvent()
                                                 {
                                                     Id = Guid.NewGuid(),
                                                     EventId = Guid.NewGuid(),
                                                     Amount = model.Amount,
                                                     CustomerId = model.CustomerId.Value,
                                                     ProductId = model.ProductId.Value,
                                                     Deadline = model.OrderDeadline,
                                                 });
            return result;
        }

    }
}
